/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年08月07日 08:53:02
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.commons.CollectionUtils;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Operator;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.BizDeptReport;
import com.pactera.jep.sys.model.BizDeptType;
import com.pactera.jep.sys.service.BizDeptReportService;
import com.pactera.jep.sys.service.BizDeptTypeService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.entity.BasicNameValuePair;
import com.pactera.jep.web.entity.NameValuePair;
import com.pactera.jep.web.util.NVUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "上报管理")
@RestController
@RequestMapping("/bizDeptReport")
public class BizDeptReportRestController extends BaseRestController<BizDeptReport> {
    Logger logger = LoggerFactory.getLogger(BizDeptReportRestController.class);

    @Autowired
    private BizDeptReportService bizDeptReportService;

    @Autowired
    private BizDeptTypeService bizDeptTypeService;

    @ApiOperation(value = "为列表页面做数据准备")
    @GetMapping(value = "/query")
    @Override
    protected void prepareQuery(String token, Page<BizDeptReport> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
        bizDeptReportService.query(page, filters, orders);
    }

    @PostMapping(value = "/save")
    public Map<String, Object> save(@RequestBody BizDeptReport bizDeptReport) {
        Map<String, Object> tips = new HashMap<String, Object>();
        Boolean isExist = bizDeptReportService.exists(bizDeptReport.getId());
        try {
            if (isExist) {
                bizDeptReportService.update(bizDeptReport);
            } else {
                bizDeptReportService.insert(bizDeptReport);
            }
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        } catch (ServiceException e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return tips;
    }

    @DeleteMapping(value = "/remove/{id}")
    public Map<String, Object> remove(@PathVariable String id) {
        Map<String, Object> tips = new HashMap<String, Object>();
        try {
            bizDeptReportService.deleteByPrimaryKey(id);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
        } catch (ServiceException e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
        }
        return tips;
    }

    @ApiOperation(value = "验证提交的id是否已经存在数据库中")
    @PostMapping(value = "/check", produces = "application/json;charset=UTF-8")
    public Map<String, Object> check(@RequestBody String id) {
        Map<String, Object> tips = new HashMap<String, Object>();
        BizDeptReport exist = bizDeptReportService.get(id);
        if (exist == null) {
            tips.put("valid", true);
        } else {
            tips.put("valid", false);
        }
        return tips;
    }


    @ApiOperation(value = "验证提交的上报部门和上报业务是否已经存在数据库中（相同的部门和相同的业务不能重复）")
    @GetMapping(value = "/checkRepeat", produces = "application/json;charset=UTF-8")
    public Map<String, Object> checkRepeat(String deptCode, String bizCode) {
        Map<String, Object> tips = new HashMap<String, Object>();
        /*查询当前菜单的兄弟菜单中是否存在公共菜单*/
        List<Filter> filters = new ArrayList<Filter>();
        filters.add(new Filter("deptCode", Operator.EQ, deptCode));
        filters.add(new Filter("bizCode", Operator.EQ, bizCode));
        List<BizDeptReport> exist = bizDeptReportService.select(filters);
        if (CollectionUtils.isEmpty(exist)) {
            tips.put("valid", true);
        } else {
            tips.put("valid", false);
        }
        return tips;
    }

    @ApiOperation(value = "根据传入的部门编码deptCode，查询其关联的业务类型，并增加参数ALL到业务类型数据中")
    @GetMapping(value = "/queryBizType")
    public List<NameValuePair> queryBizType(String deptCode) {
        List<Filter> filters = new Filter.Builder().eq("deptCode", deptCode).build();
        List<BizDeptType> result = bizDeptTypeService.select(filters);
        List<NameValuePair> nameValuePairs = NVUtils.convert(result, result.size(), "bizTypeCode", "bizTypeName");
        nameValuePairs.add(0, new BasicNameValuePair("全部", "ALL"));
        return nameValuePairs;
    }
}
